package net.googlecode.exigenlab.task5.sort;

import net.googlecode.exigenlab.task5.model.Manager;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Sorter for collection of workers
 *
 * @author Vitalii Myshenko
 * @since 12.04.11
 */
public class Sorter {

    /**
     * Sorts input Collection by personal identification number.
     *
     * @param workers Collection of managers
     */
    public Collection<Manager> sortById(Collection<Manager> workers) {
        BigDecimal minSalary, maxSalary;
        SalaryComparator salaryComparator = new SalaryComparator();
        IDComparator idComparator = new IDComparator();

        SortedSet<Manager> salarysortedworkers = new TreeSet<Manager>(salaryComparator);
        salarysortedworkers.addAll(workers);
        maxSalary = salarysortedworkers.first().getSalary();
        minSalary = salarysortedworkers.last().getSalary();
        SortedSet<Manager> idsortedworkers = new TreeSet<Manager>(idComparator);
        idsortedworkers.addAll(workers);

        Iterator<Manager> it = idsortedworkers.iterator();
        while (it.hasNext()) {
            BigDecimal workerSalary = it.next().getSalary();
            if (workerSalary == minSalary || workerSalary == maxSalary) {
                it.remove();
            }
        }
        return idsortedworkers;
    }

    /**
     * Sorts input Collection by Last name.
     *
     * @param workers Collection of managers
     */

    public Collection<Manager> sortByName(Collection<Manager> workers) {
        LastNameComparator lastNameComparator = new LastNameComparator();

        SortedSet<Manager> namesortedworkers = new TreeSet<Manager>(lastNameComparator);
        namesortedworkers.addAll(workers);
        Iterator<Manager> it = namesortedworkers.iterator();
        while (it.hasNext()) {
            if (it.next().getAge() > 45) {
                it.remove();
            }
        }
        return namesortedworkers;
    }

    /**
     * Sorts input Collection by personal salary.
     *
     * @param workers Collection of managers
     */
    public Collection<Manager> sortBySalary(Collection<Manager> workers) {
        SalaryComparator salaryComparator = new SalaryComparator();
        BigDecimal prevsalary = new BigDecimal(-1);
        SortedSet<Manager> salarysortedworkers = new TreeSet<Manager>(salaryComparator);
        salarysortedworkers.addAll(workers);

        for (Manager x : salarysortedworkers) {
            if (x.getSalary().compareTo(prevsalary) == 0) {
                prevsalary = x.getSalary();
                salarysortedworkers.remove(x);
            }
        }
        return salarysortedworkers;
    }

    /**
     * Class-comparator to compare managers to salary.
     */
    class SalaryComparator implements Comparator<Manager> {

        public int compare(Manager o1, Manager o2) {
            return (o2.getSalary().compareTo(o1.getSalary()));
        }
    }

    /**
     * Class-comparator to compare managers to personal code.
     */
    class IDComparator implements Comparator<Manager> {

        public int compare(Manager o1, Manager o2) {
            return o1.getPersonalID().compareTo(o2.getPersonalID());
        }
    }

    /**
     * Class-comparator to compare managers to Last name.
     */
    class LastNameComparator implements Comparator<Manager> {

        public int compare(Manager o1, Manager o2) {
            return o1.getLastName().compareToIgnoreCase(o2.getLastName());
        }
    }


}
